﻿using System.Windows;
using System.Windows.Controls;
using nRoute.Samples.FutureDesktop.Model;

namespace nRoute.Samples.FutureDesktop.Controls
{
    public class WorkspacesStackPanel 
        : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            // basic check
            if (this.Children.Count == 0) return base.MeasureOverride(availableSize);
            var _activatedWidth = availableSize.Width - ((this.Children.Count - 1) * 44);
            if (_activatedWidth < 44) _activatedWidth = 44;        // this ensures a min of 44 even if it exceeds the available space

            // we loop and give the widths and heights available
            foreach (UIElement _child in this.Children)
            {
                // we get the workspace
                var _workspace = (Workspace)((ContentPresenter)_child).Content;
                if (_workspace == null) continue;

                // we set the thing
                _child.Measure(new Size(_workspace.IsActive ? _activatedWidth : 44, availableSize.Height));
            }

            // we return
            return base.MeasureOverride(availableSize);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            // basic check
            if (this.Children.Count == 0) return base.ArrangeOverride(finalSize);
            var _activatedWidth = finalSize.Width - ((this.Children.Count - 1) * 44);
            if (_activatedWidth < 44) _activatedWidth = 44;        // this ensures a min of 44 even if it exceeds the available space

            // we loop and set the widths and heights
            var _point = new Point(0,0);
            foreach (UIElement _child in this.Children)
            {
                // we get the workspace
                var _workspace = (Workspace)((ContentPresenter)_child).Content;
                if (_workspace == null) continue;

                // if it is the active workspace then we give the balance width
                if (_workspace.IsActive)
                {
                    _child.Arrange(new Rect(_point, new Point(_point.X +
                        _activatedWidth, _point.Y + finalSize.Height)));
                    _point.X = _point.X + _activatedWidth;
                }
                else
                {
                    _child.Arrange(new Rect(_point, new Point(_point.X +
                        44, _point.Y + finalSize.Height)));
                    _point.X = _point.X + 44;
                }
             }

            return base.ArrangeOverride(finalSize);
        }
    }
}

